﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System.IO;
using System.Globalization;
using Autofac.Integration.Mvc;
using Autofac;
using System.Web.Configuration;
using Bids.Core.Common.IO;
using Bids.Service;

namespace Bids.Web.Controllers
{
    public class UploadController : Controller
    {
        ISystemService _iSysSrv;
        public UploadController(ISystemService iSysSrv)
        {
            _iSysSrv = iSysSrv;
        }
        public ViewResult Index()
        {
            return View();
        }

        [HttpPost]
        public string BaiDuUploadImage(string id, FormCollection form)
        {
            //上传配置
            int size = 2;           //文件大小限制,单位MB                             //文件大小限制，单位MB
            string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };         //文件允许格式

            var context = System.Web.HttpContext.Current;
            //上传图片
            Hashtable info = new Hashtable();
            BaiDuUploader up = new BaiDuUploader();

            string pathbase = null;
            int path = Convert.ToInt32(up.getOtherInfo(context, "dir"));
            if (path == 1)
            {
                pathbase = "upload/";

            }
            else
            {
                pathbase = "upload1/";
            }

            info = up.upFile(context, pathbase, filetype, size);                   //获取上传状态

            string title = up.getOtherInfo(context, "pictitle");                   //获取图片描述
            string oriName = up.getOtherInfo(context, "fileName");                //获取原始文件名


           return "{'url':'/img/" + info["url"] + "','title':'" + title + "','original':'" + oriName + "','state':'" + info["state"] + "'}";  //向浏览器返回数据json数据
        }

        public ViewResult UProduct()
        {
            return View();
        }

        public ViewResult UFile()
        {
            return View();
        }
        [HttpPost]
        public JsonResult UploadImage(string id, FormCollection form)
        {

            string savePath = "/Upload/";
            string saveUrl = "/Upload/";
            string datePre = DateTime.Now.ToString("yyMM");
            if (id != null && id != "")
            {
                savePath += id + "/" + datePre + "/";
                saveUrl += id + "/" + datePre + "/";
            }

            string dirPath = Server.MapPath(savePath);

            if (id != null && id != "")
            {
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
            }

            string fileTypes = "gif,jpg,jpeg,png,bmp";
            int maxSize = 1000000;
            Hashtable hash = new Hashtable();
            HttpPostedFileBase file;
            var n = Request["n"];
            if (n=="0")
            {
                file = Request.Files[0];
            }
            else
            {
                 file = Request.Files["imgFile"];
            }

            if (file == null)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "请选择文件";
                return Json(hash, "text/html;charset=UTF-8");
            }

            if (!Directory.Exists(dirPath))
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传目录不存在";
                return Json(hash, "text/html;charset=UTF-8");
            }
            string fileName = file.FileName;
            string fileExt = Path.GetExtension(fileName).ToLower();
            ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(','));
            if (file.InputStream == null || file.InputStream.Length > maxSize)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传文件大小超过限制";
                return Json(hash, "text/html;charset=UTF-8");
            }
            if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传文件扩展名是不允许的扩展名";
                return Json(hash, "text/html;charset=UTF-8");
            }

            //删除旧图片
            //if (form["oldimageurl"] != null && form["oldimageurl"].ToString().Trim() != "")
            //{
            //    string oldPath = Server.MapPath(form["oldimageurl"].ToString().Trim());
            //    System.IO.File.Delete(oldPath);
            //}

            string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
            string filePath = dirPath + newFileName;
            file.SaveAs(filePath);
            string fileUrl = "/img/" + saveUrl + newFileName;
            hash = new Hashtable();
            hash["error"] = 0;
            hash["url"] = fileUrl;
            return Json(hash, "text/html;charset=UTF-8");
        }
        [HttpPost]
        public JsonResult UploadFile(string id, FormCollection form)
        {

            string savePath = "/Upload/";
            string saveUrl = "/Upload/";
            string datePre = DateTime.Now.ToString("yyMM");
            if (id != null && id != "")
            {
                savePath += id + "/" + datePre + "/";
                saveUrl += id + "/" + datePre + "/";
            }

            string dirPath = Server.MapPath(savePath);

            if (id != null && id != "")
            {
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
            }

            string fileTypes = "JPG,PNG,GIF,PDF,EPS,TXT,ODT";
            int maxSize = 1000000000;
            Hashtable hash = new Hashtable();
            HttpPostedFileBase file = Request.Files[0];


            if (file == null)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "请选择文件";
                return Json(hash, "text/html;charset=UTF-8");
            }

            if (!Directory.Exists(dirPath))
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传目录不存在";
                return Json(hash, "text/html;charset=UTF-8");
            }
            string fileName = file.FileName;
            string fileExt = Path.GetExtension(fileName).ToLower();
            ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(','));
            if (file.InputStream == null || file.InputStream.Length > maxSize)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传文件大小超过限制";
                return Json(hash, "text/html;charset=UTF-8");
            }
            if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传文件扩展名是不允许的扩展名";
                return Json(hash, "text/html;charset=UTF-8");
            }

            //删除旧图片
            //if (form["oldimageurl"] != null && form["oldimageurl"].ToString().Trim() != "")
            //{
            //    string oldPath = Server.MapPath(form["oldimageurl"].ToString().Trim());
            //    System.IO.File.Delete(oldPath);
            //}

            string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
            string filePath = dirPath + newFileName;
            file.SaveAs(filePath);
            string fileUrl = saveUrl + newFileName;
            hash = new Hashtable();
            hash["error"] = 0;
            hash["url"] = fileUrl;
            return Json(hash, "text/html;charset=UTF-8");
        }

        
    }
}
